const {
  getEventData
} = getApp()
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  properties: {
    title: {
      type: String,
      value: ''
    },
    value: {
      type: null,
      value: ''
    },
    required: {
      type: Boolean,
      value: false
    },
    placeholder: {
      type: String,
      value: '请选择',
    },
    inset: {
      type: Boolean,
      value: false
    },
    nodeKey: {
      type: String,
      value: 'name'
    },
    valueKey: {
      type: String,
      value: 'text'
    },
    options: Array,
    showLink: Boolean,
    disabled: Boolean,
    showClear: {
      type: Boolean,
      value: true
    },
    useRightIcon: Boolean
  },
  data: {
    pickerValue: ''
  },
  methods: {
    getValue(index) {
      const item = this.data.options[+index];
      return item[this.data.nodeKey];
    },
    bindPickerChange(event) {
      this.setData({
        value: this.getValue(event.detail.value)
      })

      this.triggerEvent('change', this.data.value)
    },


    onClick() {
      this.triggerEvent('click')
      this.setData({
        pickerValue: this.data.options.findIndex(item => item[this.data.nodeKey] === this.data.value)
      })
    },


    onClear() {
      this.setData({
        value: ''
      })
      this.triggerEvent('clear')
      this.triggerEvent('change', this.data.value)
    },
  }
})